﻿package edu.mit.media.fv.layers
{
	import edu.mit.media.fv.blocks.*;
	import edu.mit.media.fv.components.*;
	import edu.mit.media.fv.events.*;
	import edu.mit.media.fv.fluidspace.*;
	import edu.mit.media.fv.icons.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.filters.*;
	import flash.geom.Rectangle;
	import flash.text.*;
	
	
	public class Layer extends Sprite implements BlockContainer
	{
		public static var arc:int = 15;
		public static var MARGIN:int = 10;
		public static var HEADER_HEIGHT:int = 10;
		public static var ICON_WIDTH:int = 50;
		public static var filter:BevelFilter = new BevelFilter(4, 45,0xFFFFFF, 1, 0x000000, 1);
		
		private var _active:Boolean;
		private var _backgroundColor:uint;
		private var _layerWidth:int;
		private var _layerHeight:int;
		
		protected var _viewbounds:Rectangle;
		protected var _icon:DisplayObject;
		protected var _label:TextField;
		protected var _wall:Sprite;
		protected var _canvas:Sprite;
		protected var _blocks:Array;//array of Blocks
		
		public function Layer(x:int = 0, y:int = 0, width:int = 650, height:int = 100, icon:DisplayObject = null){
			super();
			this.x = x;
			this.y = y;
			this._layerWidth = width;
			this._layerHeight = height;
			this._blocks = new Array();
			this._active = true;
			this._backgroundColor = 0xFFFFFF;
			this._viewbounds = new Rectangle();
			this._icon = icon;
			FluidEventHandler.dispatchEvent(new BlockEvent(BlockEvent.CONTAINER_ADDED, null, this));
		}
		
		//=============================>
		//setters and getters
		//=============================>
		public function get active():Boolean{
			return _active;
		}
		public function set active(active:Boolean):void{
			this._active = active;
		}
		public function get layerWidth():int {
			return this._layerWidth;
		}
		public function set layerWidth(width:int):void {
			this._layerWidth = width;
			this.reform();
		}
		public function get layerHeight():int {
			return this._layerHeight;
		}
		public function set layerHeight(height:int):void {
			this._layerHeight = height;
			this.reform();
		}
		public function get containerWidth():Number{
			return this._layerWidth;
		}
		public function get containerHeight():Number{
			return this._layerHeight;
		}
		public function get backgroundColor():uint {
			return this._backgroundColor;
		}
		public function set backgroundColor(color:uint):void {
			this._backgroundColor = color;
			this.reform();
		}
		public function get label():String {
			return this._label.text;
		}
		public function set label(text:String):void {
			if(this._label != null){
				this._label.text = text;
			}
			this.reform();
		}
		public function get viewbounds():Rectangle{
			return this._viewbounds;
		}
		public function set viewbounds(r:Rectangle):void{
			this._viewbounds = new Rectangle(r.x, r.y, r.width, r.height);
		}
		public function get blocks():Array{
			return _blocks.slice();
		}
		//return ids of blocks
		public function get blockIDs():Array{
			var ids:Array = new Array();
			for (var i:int = 0; i<this._blocks.length; i++){
				var block:Block = Block(this._blocks[i]);
				ids.push(block.model.id);
			}
			return ids;
		}

		
		//=============================>
		//Logic and Modifieres
		//=============================>
		
		public function reform():void{
			return;
		}
		public function hasBlock(block:Block):Boolean{
			if(this.blocks.indexOf(block) == -1){
				return false;
			}else{
				return true;
			}
		}
		public function removeBlock(block:Block):void{
			var i:int = this._blocks.indexOf(block);
			if(i != -1){
				this._blocks.splice(i, 1);
			}
			if(block.parent != null){
				block.parent.removeChild(block);
			}
			
		}
		public function addBlock(block:Block):void{
			if(block == null){
				trace("cannot add null blockk instance");
				return;
			}
			block.blockContainer = this;
			var index:int = this._blocks.indexOf(block);
			if(index < 0 || index >= this._blocks.length){
				this._blocks.push(block);
			}
			block.x = Math.max(0, Math.min(block.x, this._viewbounds.width-block.diameter));
			block.y = Math.max(0, Math.min(block.y, this._viewbounds.height-block.diameter));
		}
		public function dropBlock(block:Block):void{
			
		}
		public function clearBlocks():void{
			this._blocks = new Array();
			for(var i:int = 0; i<this._blocks.length; i++){
				var block:Block = Block(this._blocks[i]);
				if(block.parent != null){
					block.parent.removeChild(block);
				}
			}
		}
		public function containsPoint(x:Number, y:Number, shapeFlag:Boolean = false):Boolean{
			if(this._wall != null){
				return this._wall.hitTestPoint(x, y, shapeFlag);
			}else{
				return false;
			}
		}
	}
}